package service

import (
	"context"
	pb "doctor-srv/basic/proto"
	"doctor-srv/basic/config"
	"doctor-srv/handler/model"
)

type MedicalRecordService struct {
	pb.UnimplementedMedicalRecordServer
}

// CreateMedicalRecord 创建病历
func (s *MedicalRecordService) CreateMedicalRecord(ctx context.Context, req *pb.CreateMedicalRecordReq) (*pb.CreateMedicalRecordResp, error) {
	record := model.MedicalRecord{
		RecordNo:       GenerateOrderNo("MR"),
		ConsultationID: req.ConsultationId,
		DoctorID:       req.DoctorId,
		PatientID:      req.PatientId,
		Diagnosis:      req.Diagnosis,
		ChiefComplaint: req.ChiefComplaint,
		PresentIllness: req.PresentIllness,
		PastHistory:    req.PastHistory,
		PhysicalExam:   req.PhysicalExam,
		Treatment:      req.Treatment,
		TemplateID:     req.TemplateId,
	}

	if err := record.CreateMedicalRecord(config.DB); err != nil {
		return nil, err
	}

	return &pb.CreateMedicalRecordResp{
		RecordId: int64(record.ID),
		RecordNo: record.RecordNo,
	}, nil
}

// GetMedicalRecordList 获取病历列表
func (s *MedicalRecordService) GetMedicalRecordList(ctx context.Context, req *pb.GetMedicalRecordListReq) (*pb.GetMedicalRecordListResp, error) {
	records, total, err := model.GetMedicalRecordList(
		config.DB,
		req.PatientId,
		req.DoctorId,
		req.StartDate,
		req.EndDate,
		int(req.Page),
		int(req.PageSize),
	)
	if err != nil {
		return nil, err
	}

	var recordInfos []*pb.MedicalRecordInfo
	for _, record := range records {
		// 获取患者和医生信息
		var patient model.Patient
		var doctor model.Doctor
		patient.GetPatientByID(config.DB, record.PatientID)
		doctor.GetDoctorByID(config.DB, record.DoctorID)

		recordInfos = append(recordInfos, &pb.MedicalRecordInfo{
			RecordId:    int64(record.ID),
			RecordNo:    record.RecordNo,
			PatientId:   record.PatientID,
			PatientName: patient.Name,
			DoctorId:    record.DoctorID,
			DoctorName:  doctor.Name,
			Diagnosis:   record.Diagnosis,
			Treatment:   record.Treatment,
			CreatedAt:   record.CreatedAt.Unix(),
		})
	}

	return &pb.GetMedicalRecordListResp{
		Records: recordInfos,
		Total:   total,
	}, nil
}

// GetMedicalRecordDetail 获取病历详情
func (s *MedicalRecordService) GetMedicalRecordDetail(ctx context.Context, req *pb.GetMedicalRecordDetailReq) (*pb.GetMedicalRecordDetailResp, error) {
	var record model.MedicalRecord
	err := record.GetMedicalRecordByID(config.DB, req.RecordId)
	if err != nil {
		return nil, err
	}

	// 获取患者和医生信息
	var patient model.Patient
	var doctor model.Doctor
	patient.GetPatientByID(config.DB, record.PatientID)
	doctor.GetDoctorByID(config.DB, record.DoctorID)

	return &pb.GetMedicalRecordDetailResp{
		Record: &pb.MedicalRecordInfo{
			RecordId:    int64(record.ID),
			RecordNo:    record.RecordNo,
			PatientId:   record.PatientID,
			PatientName: patient.Name,
			DoctorId:    record.DoctorID,
			DoctorName:  doctor.Name,
			Diagnosis:   record.Diagnosis,
			Treatment:   record.Treatment,
			CreatedAt:   record.CreatedAt.Unix(),
		},
	}, nil
}

// ExportMedicalRecord 导出病历
func (s *MedicalRecordService) ExportMedicalRecord(ctx context.Context, req *pb.ExportMedicalRecordReq) (*pb.ExportMedicalRecordResp, error) {
	var record model.MedicalRecord
	err := record.GetMedicalRecordByID(config.DB, req.RecordId)
	if err != nil {
		return nil, err
	}

	// TODO: 生成PDF文件
	fileUrl := "http://example.com/medical_records/" + record.RecordNo + ".pdf"

	return &pb.ExportMedicalRecordResp{
		FileUrl: fileUrl,
	}, nil
}

// RateMedicalRecord 评价病历
func (s *MedicalRecordService) RateMedicalRecord(ctx context.Context, req *pb.RateMedicalRecordReq) (*pb.RateMedicalRecordResp, error) {
	var record model.MedicalRecord
	err := record.GetMedicalRecordByID(config.DB, req.RecordId)
	if err != nil {
		return &pb.RateMedicalRecordResp{
			Success: false,
			Message: "病历不存在",
		}, err
	}

	record.Rating = int(req.Rating)
	record.RatingComment = req.Comment

	if err := record.UpdateMedicalRecord(config.DB); err != nil {
		return &pb.RateMedicalRecordResp{
			Success: false,
			Message: "评价失败",
		}, err
	}

	return &pb.RateMedicalRecordResp{
		Success: true,
		Message: "评价成功",
	}, nil
}

